package com.codechuan.hallo_internal.ui.myOrder;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ListView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.codechuan.hallo_internal.R;
import com.codechuan.hallo_internal.adapter.OrderBaseAdapter;
import com.codechuan.hallo_internal.common.Constant;
import com.codechuan.hallo_internal.ui.login.LoginActivity;
import com.codechuan.hallo_internal.utils.ContextUtil;
import com.codechuan.hallo_internal.utils.DialogUtil;
import com.codechuan.hallo_internal.utils.JDBCUtil;
import com.codechuan.hallo_internal.utils.MyProjectHttpUtil;

import java.util.ArrayList;

public class MyOrder extends AppCompatActivity {

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_my_order);

        // 创建自定义适配器对象
        ListView listView = findViewById(R.id.orderListData);
        OrderBaseAdapter orderBaseAdapter = new OrderBaseAdapter(this, Constant.ORDER_LIST);
        listView.setAdapter(orderBaseAdapter);
        // 获取删除
        Button deleteAllOrder = findViewById(R.id.deleteAllOrder);
        deleteAllOrder.setOnClickListener(view -> Constant.executorService.execute(new Runnable() {
            @Override
            public void run() {
                // 执行删除所有订单
                boolean b = MyProjectHttpUtil.deleteOrderInfo();
                Constant.handler.post(()->{
                    DialogUtil.showDialog(ContextUtil.context,"删除成功!");
                    // 置空订单
                    Constant.ORDER_LIST = new ArrayList<>();

                    Intent intent = new Intent();
                    intent.setClass(ContextUtil.context, MyOrder.class);
                    startActivity(intent);
                });
            }
        }));
        // 返回首页
        Button returnHome = findViewById(R.id.returnHome);
        returnHome.setOnClickListener(view -> Constant.handler.post(()->{
            // 跳转到登录页
            Intent intent = new Intent();
            intent.setClass(MyOrder.this, LoginActivity.class);
            startActivity(intent);
        }));

    }
}